package com.service.information.imp;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.configration.TagMapper;
import com.mapper.information.InformationMapper;
import com.po.DictionaryInfo;
import com.po.InformationInfo;
import com.po.SearchInfo;
import com.po.TagInfo;
import com.service.information.InformationService;
import com.util.Similarity;
import com.util.UUIDGenerator;

@Service
public class InformationServiceImp implements InformationService {
	@Resource(name = "informationMapper")
	private InformationMapper informationMapper;
	 @Resource(name="tagMapper")
	 private TagMapper tagMapper;
	@Override
	public List<InformationInfo> getInformationList(InformationInfo informationInfo) {
		List<InformationInfo> list = informationMapper.getInformationList(informationInfo);
		return list;
	}
	//首页推荐
	public List<InformationInfo> getRecommendList(){
		
		List<InformationInfo> list = informationMapper.getRecommendList();
		return list;

	}
	@Override
	public int deleteInformations(String[] ids) {
		int i = informationMapper.deleteInformations(ids);
		return i;
	}

	@Override
	public int deleteInformations(InformationInfo informationInfo) {
		int i = informationMapper.deleteInformation(informationInfo);
		return i;
	}

	@Override
	public int getInformationCount(InformationInfo informationInfo) {
		int i = informationMapper.getInformationCount(informationInfo);
		return i;
	}

	@Override
	public List<InformationInfo> getSearchList(int offset, int limit, String category, String content,
			String[] reczsk_id, String tag_name, String time) {
		List<InformationInfo> list = informationMapper.getSearchList(offset, limit, category, content, reczsk_id,
				tag_name, time);
		return list;
	}

	@Override
	public int getSearchCount(String category, String content, String[] reczsk_id, String tag_name, String time) {
		int i = informationMapper.getSearchCount(category, content, reczsk_id, tag_name, time);
		return i;
	}

	@Override
	public List<InformationInfo> getADSearchList(int offset, int limit, List<SearchInfo> orlist,
			List<SearchInfo> andlist, String[] reczsk_id, String tag_name, String time) {
		List<InformationInfo> listt = informationMapper.getADSearchList(offset, limit, orlist, andlist, reczsk_id,
				tag_name, time);
		return listt;
	}

	@Override
	public int getADSearchCount(int offset, int limit, List<SearchInfo> orlist,
			List<SearchInfo> andlist, String[] reczsk_id, String tag_name, String time) {
		int i = informationMapper.getADSearchCount(offset, limit, orlist, andlist, reczsk_id,
				tag_name, time);
		return i;
	}

	@Override
	public InformationInfo getOneInformation(String info_id) {
		InformationInfo info = informationMapper.getOneInformation(info_id);
		return info;
	}

	@Override
	public int addUploadinfo(InformationInfo information) {
		// TODO Auto-generated method stub
		int i = informationMapper.addUploadinfo(information);
		return i;
	}
	

	@Override
	public int addInformationList(List<InformationInfo> info) {
		// TODO Auto-generated method stub
		int i = informationMapper.addInformationList(info);
		return i;
	}

	@Override
	public int updateInformation(InformationInfo info) {
		// TODO Auto-generated method stub
		int i = informationMapper.updateInformation(info);
		return i;
	}

	public int updateBrowser(String infoid){
		
		int i = informationMapper.updateBrowser(infoid);
		return i;
	}

	@Override
	public int updateDownload(String infoid,String userid) {
		int result = 0;
		int i = informationMapper.updateDownload(infoid);
		int j = informationMapper.addDownload(userid, infoid);
		if(i>0&&j>0){
			result = 1;
		}
		return result;
	}
	@Override
	public List<InformationInfo> getInfoManage(String infoname,String kudata,String infolevel,int offset,int page) {
		// TODO Auto-generated method stub
		List<InformationInfo> list = informationMapper.getInfoManage(infoname,kudata,infolevel,offset,page);
		return list;
	}
	@Override
	public int getInfoManageCount(String infoname,String kudata,String infolevel) {
		int i = informationMapper.getInfoManageCount(infoname,kudata,infolevel);
		return i;
	}
	@Override
	public int updateAccess() {
		// TODO Auto-generated method stub
		String date = UUIDGenerator.getDateString();
		int i = informationMapper.updateAccess(date);
		return i;
	}
	

	@Override
	public int updateInformationKu(String origin, String destination) {
		int j = informationMapper.getInformationKuCount(origin);
		int i = informationMapper.updateInformationKu(origin, destination);
		if (i == j)
			return 1;
		else
			return 0;
	}

	@Override
	public int getInformationKuCount(String origin) {
		int i = informationMapper.getInformationKuCount(origin);
		return i;
	}
	@Override
	public List<DictionaryInfo> getSearchYearList(String category, String content, String reczsk_id, String tag_name,
			String time) {
		// TODO Auto-generated method stub
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<DictionaryInfo> list = informationMapper.getSearchYearList(category, content, reczsk_ids, tag_name, time);
		return list;
	}
	@Override
	public List<DictionaryInfo> getADSearchYearList(List<SearchInfo> orlist, List<SearchInfo> andlist,
			String reczsk_id, String tag_name, String time) {
		// TODO Auto-generated method stub
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<DictionaryInfo> list = informationMapper.getADSearchYearList(orlist, andlist, reczsk_ids, tag_name, time);
		return list;
	}
	@Override
	public int updateInfolevel( String level,String infoid) {
		// TODO Auto-generated method stub
		int result = informationMapper.updateInfolevel(level, infoid);
		return result;
	}
	@Override
	public int updateInfoslevel(String level, String infoids) {
		// TODO Auto-generated method stub
		String[] infoid={};
		if (infoids != "" && infoids != null) {
			infoid = infoids.split(",");
		}   
		
		int result = informationMapper.updateInfoslevel(level, infoid);
		return result;
	}
	@Override
	public int getInformation(InformationInfo info) {
		// TODO Auto-generated method stub
		int result = informationMapper.getInformation(info);
		return result;
	}
	@Override
	public int updateInfosclass(String infoids, String kudata) {
		// TODO Auto-generated method stub
		String[] infoid={};
		if (infoids != "" && infoids != null) {
			infoid = infoids.split(",");
		}
		int result = informationMapper.updateInfoclass(kudata, infoid);
		return result;
	}
	@Override
	public List<InformationInfo> getInfoExcel(String infoname, String kudata, String infolevel) {
		// TODO Auto-generated method stub
		List<InformationInfo> list = informationMapper.getInfoExcel(infoname, kudata, infolevel);
		return list;
	}
	@Override
	public List<InformationInfo> getInfokeywords(String infoid) {
		// TODO Auto-generated method stub
		List<InformationInfo> list = informationMapper.getInfokeywords(infoid);
		return list;
	}
	//根据文本相似度推荐
	public List<InformationInfo> getWbrecommendList(String sourcestr,String infoid){
		
		List<InformationInfo> infokeylist = informationMapper.getInfokeywords(infoid);
		for(InformationInfo info:infokeylist){
			String tocomparestr = info.getInfo_keyword();
			double similarity = Similarity.getSimilarity(sourcestr,tocomparestr);
			info.setSimilarity(similarity);
		}
        Collections.sort(infokeylist, (a, b) -> b.compareTo(a));
        if(infokeylist.size()<=12)
        	return infokeylist;
        else
        	return infokeylist.subList(0, 12);

	}
	@Override
	public List<TagInfo> getSearchTagList(String category, String content, String reczsk_id, String tag_name,
			String time) {
		// TODO Auto-generated method stub
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<TagInfo> taglist = tagMapper.getTagListByReczsk(reczsk_ids);
		int i = 0;
		for (TagInfo tag : taglist) {
			List<DictionaryInfo> dic_list  = informationMapper.getSearchTagList(category, content, reczsk_ids, tag_name, time,tag.getTag_id() );
			taglist.get(i).setList(dic_list);
			i++;
		}
		return taglist;
	}
	@Override
	public List<TagInfo> getADSearchTagList(List<SearchInfo> orlist, List<SearchInfo> andlist, String reczsk_id,
			String tag_name, String time) {
		// TODO Auto-generated method stub
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<TagInfo> taglist = tagMapper.getTagListByReczsk(reczsk_ids);
		int i = 0;
		for (TagInfo tag : taglist) {
			List<DictionaryInfo> dic_list  = informationMapper.getADSearchTagList(orlist, andlist, reczsk_ids, tag_name, time,tag.getTag_id() );
			taglist.get(i).setList(dic_list);
			i++;
		}
		return taglist;
	}
}
